import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;


@WebServlet("/login")
public class LoginServlet extends HttpServlet {

    //用户点击提交按钮，服务器运行dopost方法
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取用户提价的信息，用户名，密码
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //3.设置浏览器显示编码
        resp.setContentType("text/html;charset=utf-8");
        //2.判断用户名和密码是否正确
        if("zhangsan".equals(username)  && "123".equals(password)){
            //登录成功，则创建会话保存必要的身份信息
            HttpSession session = req.getSession(true);
            //往session里面保存数据
            session.setAttribute("username",username); //用户名
            session.setAttribute("password",password); //密码
            session.setAttribute("count",0);//登录次数
//            //之后返回登录成功
//            resp.getWriter().write("登录成功");


            //index页面
            resp.sendRedirect("index");

        }else {
            resp.getWriter().write("登录失败");
        }






    }
}
